<template>
    <div>
        <el-dialog title="绑定BOM到工单产品" v-model="dialogVisible" width="50%" @close="handleClosed">

            <div class="main-title">BOM列表</div>
            <el-table :data="bomInfos" style="width: 100%" height="150">
                <el-table-column prop="name" label="物料名称"/>
                <el-table-column prop="unitName" label="单位"/>
                <el-table-column prop="addCount" label="数量"/>
            </el-table>

            <el-form inline style="margin-top: 20px;">
                <el-form-item label="客户名称">
                    <el-input v-model="param.customerName" placeholder="请输入客户名称"/>
                </el-form-item>
                <el-form-item label="产品规格">
                    <el-input v-model="param.productSpecification" placeholder="输入产品规格"/>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleSelect">搜索</el-button>
                </el-form-item>
            </el-form>
            <div class="main-title">工单列表</div>
            <el-table :data="queryData" style="width: 100%" height="200" @select="handleSelectedChanged">
                <el-table-column type="selection"></el-table-column>
                <el-table-column prop="orderId" label="工单号" width="180"></el-table-column>
                <el-table-column prop="customerName" label="客户名称" width="180"></el-table-column>
                <el-table-column prop="productSpecification" label="产品规格" width="180"></el-table-column>
                <el-table-column prop="employeeName" label="负责人" width="180"></el-table-column>
                <el-table-column prop="remarks" label="备注"></el-table-column>

            </el-table>
            <div class="footer">
                <el-button type="primary" @click="handleBind">绑定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import mdAPI from "@/util/request-md/mat-pro/mat-pro.js";
import bomAPI from "@/util/request-md/bom/bom.js";

export default {
    emits: ['callClosed'],
    props: ['visible', 'bomInfos', 'productId'],
    data() {
        return {
            param: {
                customerName: '',
                productSpecification: '',
                employeeName: '',
                remarks: ''
            },
            dialogVisible: false,
            mainCount: 0,
            queryData: [],
            selOrder: [],
            orders: [
                {
                    orderId: '123',
                    customerName: '客户A',
                    productSpecification: '产品A',
                    employeeName: "张伟",
                    remarks: '备注信息'
                },

            ]
        }
    },
    watch: {
        visible(val) {
            this.dialogVisible = val;
        }
    },
    methods: {
        handleClosed() {
            this.$emit('callClosed');
        },
        //选中工单时触发
        handleSelectedChanged(data) {
            this.selOrder = data;
        },
        //添加bom信息
        handleBind() {
            this.selOrder.forEach(sel=>{
                this.bomInfos.forEach(bom=>{
                    var queryData={
                        matId:bom.id,
                        orderId:sel.orderId,
                        prop:bom.addCount
                    }
                    console.log(queryData,">>>>productId"+this.productId)
                    bomAPI.addOrderBom(queryData,this.productId,((res)=>{
                        if(res.code === 200){
                            this.$message({
                                message: '绑定成功',
                                type: 'success'
                            });
                        }
                    }))
                })

            })
        },
        //加载该产品的工单信息
        loadOrders() {
            mdAPI.getProductOrders(this.productId, ((res) => {
                if (res.code === 200) {
                    this.orders = res.data;
                    // console.log(this.orders,">>>>拿到了数据ORDER数据")
                    this.queryData = this.orders;
                }
            }));
        },
        handleSelect() {
            this.queryData = this.orders;
            //根据param在orders中做本地搜索
            if (this.param.productSpecification === "" && this.param.customerName === "") {
                this.queryData = this.orders;
            } else {
                this.queryData = this.orders.filter(item => {
                    return item.customerName.includes(this.param.customerName) && item.productSpecification.includes(this.param.productSpecification)
                })
            }
        }


    }
    ,
    mounted() {
        this.loadOrders();
        // console.log(this.productId,">>>>绑定时的productId");
    }
}
</script>
<style scoped>
.footer {
    margin-top: 30px;
    margin-bottom: 10px;
    text-align: center;
}
</style>